package com.xxq.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import my.mvc.Annotation.JSONOutput;
import my.mvc.MultipartRequest;
import my.mvc.RequestContext;
import my.util.ResponseUtils;
import my.util.ServletUtils;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.xxq.bean.Record;
import com.xxq.dao.support.Page;
import com.xxq.entity.Mission;
import com.xxq.json.Jackson;
import com.xxq.service.MissionService;


public class MissionAction {

	private MissionService missionService = new MissionService();

	/**
	 * 增加或修改班级
	 * */
	public void save(RequestContext rc) throws ServletException, IOException {
		// 将请求参数自动包装成Bean对象
		Mission mission = rc.form(Mission.class);
		
		if(mission.getId()==0) {
			// 增加学生
			missionService.add(mission);
		} else {
			// 修改学生
			missionService.update(mission);
		}
		
		// 输出JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	/**
	 * 列表
	 * */
	@JSONOutput
	public Record list(RequestContext rc) throws ServletException, IOException {
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 15);
		String orderBy = rc.param("orderBy", "id");
		String orderDir = rc.param("orderDir", "asc");
		
		Page<Mission> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		Record record = missionService.findPage(page);
		// 输出JSON
//		String json = Jackson.me().toJson(record);
//		ResponseUtils.renderJson(rc.response(), json);
		
		return record;
	}
	
	/**
	 * 删除
	 * */
	public void del(RequestContext rc) throws ServletException, IOException {
		long id = rc.param("id", 0L);
		missionService.remove(id);
		
		// 返回JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	/**
	 * 修改之前，获得信息，用于回显
	 * */
	public void get(RequestContext rc) throws ServletException, IOException {
		String id = rc.param("id");
		if(id!=null && !id.equals("")) {
			Mission mission = missionService.findById(Long.parseLong(id));
			rc.reqAttr("mission", mission);
			rc.forward("/admin/mission_form.jsp");
		}
	}
	
	/**
	 * 批量删除
	 * */
	@JSONOutput
	public Record multiDel(RequestContext rc) throws ServletException, IOException {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			missionService.multiDel(ids);
		}		
		return new Record(true);
	}
}
